from django.core.exceptions import ObjectDoesNotExist
from rest_framework import serializers
from .models import Grade, GradeDetail
from ..PaperManagement.models import PaperQuestion


class GradeSerializer(serializers.ModelSerializer):
    exam_title = serializers.CharField(source='exam.title', read_only=True)
    average_score = serializers.IntegerField(read_only=True)
    pass_rate = serializers.DecimalField(max_digits=5, decimal_places=2, read_only=True)
    should_num = serializers.IntegerField(read_only=True)
    actual_num = serializers.IntegerField(read_only=True)

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.order = 1
        self.exam = None

    class Meta:
        model = Grade
        fields = ["id", "exam_title", "average_score", "pass_rate", "should_num", "actual_num"]



    def to_representation(self, instance):
        # 获取原始序列化数据
        representation = super().to_representation(instance[0])

        exam = instance[0].exam
        if exam != self.exam and self.exam is not None:
            self.order += 1
        self.exam = exam

        representation["errorRates"] = instance[1]
        representation["order"] = self.order

        return representation


class DetailGradeSerializer(serializers.Serializer):
    name = serializers.CharField(read_only=True)
    user_id = serializers.CharField(read_only=True)
    exam_id = serializers.CharField(read_only=True)
    depart = serializers.CharField(read_only=True)
    total_score = serializers.IntegerField(read_only=True)
    correct_rate = serializers.DecimalField(max_digits=5, decimal_places=2, read_only=True)
    created_at = serializers.DateTimeField(read_only=True)
    exam_duration = serializers.CharField(read_only=True)



# class ExamGradeDetailSerializer(serializers.ModelSerializer):
#     question_topic = serializers.CharField(source='question.topic', read_only=True)
#     correct_rate = serializers.DecimalField(max_digits=5, decimal_places=2, read_only=True)
#     exam_title = serializers.CharField(source='exam.title', read_only=True)
#     order = serializers.SerializerMethodField()
#
#     def __init__(self, *args, **kwargs):
#         super().__init__(*args, **kwargs)
#         self.order = 0
#         self.exam = None
#
#     class Meta:
#         model = GradeDetail
#         fields = ["id", "question_topic", "correct_rate", "exam_title","order"]
#
#     def get_order(self, obj):
#         exam = obj.exam
#         if exam != self.exam:
#             self.order = 0
#
#         self.exam = exam
#         self.order += 1
#         return self.order
#
#     def to_representation(self, instance):
#
#         # 获取原始序列化数据
#         representation = super().to_representation(instance)
#         print(representation)  #  OrderedDict([('id', '8926d4ed-f15b-4a43-8087-0d47386b819d'), ('question_topic', '1+1等于多少'), ('correct_rate', '0.00'), ('exam_title', '三道题的考试'), ('order', 2)])
#         #  OrderedDict([('id', '8926d4ed-f15b-4a43-8087-0d47386b819d'), ('question_topic', '1+1等于多少'), ('correct_rate', '0.00'), ('exam_title', '三道题的考试'), ('order', 2)])
#
#
#
#         print(representation)  #  OrderedDict([('id', '8926d4ed-f15b-4a43-8087-0d47386b819d'), ('question_topic', '1+1等于多少'), ('correct_rate', '0.00'), ('exam_title', '三道题的考试'), ('order', 2), ('index', 2)])
#
#         return representation
